﻿package com.thorqq.magictimer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.thorqq.magictimer.MagicTimerActivity.ListenerMore;
import com.thorqq.magictimer.MagicTimerActivity.ListenerNameInfo;
import com.thorqq.magictimer.MagicTimerActivity.ListenerTime;
import com.thorqq.magictimer.MagicTimerActivity.TimerLongClickListener;
import com.thorqq.magictimer.animation.ExpandAnimation;
import com.thorqq.magictimer.core.ChildView;
import com.thorqq.magictimer.core.ChildViewParent;
import com.thorqq.magictimer.timeLoop.Timer;
import com.thorqq.magictimer.util.DateUtil;
import com.thorqq.magictimer.widget.BadgeView;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class AdapterTimerItem extends BaseAdapter
{

    public final class ViewHolder
    {
        public boolean mInit = false;
        
        public Timer timer;
        public ChildView childView;
        public int visibility = View.GONE;
        public ChildViewParent parent;
        public ExpandAnimation expandAni;

        public TextView time;
        public ImageView imageViewToggle;
        public TextView name;
        public TextView info;
        public ImageView more;
        public BadgeView badge;

        public ViewGroup layoutTime;
        public ViewGroup layoutNameAndInfo;
        public ViewGroup layoutDown;
        public ViewGroup layoutMore;
    }

    private LayoutInflater mInflater;
    private List<Map<String, Object>> mData;
    @SuppressWarnings("unused")
    private Context mContext;

    public AdapterTimerItem(Context context)
    {
        mInflater = LayoutInflater.from(context);
        mData = new ArrayList<Map<String, Object>>();
        mContext = context;
    }

    public AdapterTimerItem(Context context, List<Map<String, Object>> data)
    {
        mInflater = LayoutInflater.from(context);
        mData = data;
        mContext = context;
    }

//    public void add(Map<String, Object> map)
//    {
//        mData.add(map);
//    }
//
//    public void updateItem(int position, String key, Object obj)
//    {
//        if (position < mData.size())
//        {
//            mData.get(position).put(key, obj);
//        }
//    }

    public int getCount()
    {
        return mData.size();
    }

    public Object getItem(int position)
    {
        if (position < mData.size())
        {
            return mData.get(position);
        } else
        {
            return null;
        }
    }

    public long getItemId(int position)
    {
        if (position < mData.size())
        {
            return position;
        } else
        {
            return -1;
        }
    }

    public View getView(final int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;

        ListenerTime lstTime = null;
        ListenerNameInfo lstNameInfo = null;
        ListenerMore lstMore = null;

        if (convertView == null)
        {
            holder = new ViewHolder();

            // 布局
            convertView = mInflater.inflate(R.layout.timer_item, null);
            holder.time = (TextView) convertView.findViewById(R.id.TimerItemTime);
            holder.imageViewToggle = (ImageView) convertView.findViewById(R.id.ImageViewToggle);
            holder.name = (TextView) convertView.findViewById(R.id.TimerItemName);
            holder.info = (TextView) convertView.findViewById(R.id.TimerItemInfo);
            holder.more = (ImageView) convertView.findViewById(R.id.TimerItemMore);
            holder.badge = (BadgeView) convertView.findViewById(R.id.TimerItemBadge);
            holder.badge.setBadgeBackgroundColor(0x40ffffff);
            holder.badge.setTextColor(Color.WHITE);

            holder.layoutTime = (LinearLayout) convertView.findViewById(R.id.RelativeLayoutTime);
            holder.layoutNameAndInfo = (LinearLayout) convertView
                    .findViewById(R.id.RelativeLayoutNameAndInfo);
            holder.layoutDown = (LinearLayout) convertView.findViewById(R.id.RelativeLayoutDown);
            holder.layoutMore = (LinearLayout) convertView.findViewById(R.id.LayoutTimerItemMore);

            convertView.setTag(holder);
        } else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        // 传递过来的数据
        holder.timer = (Timer) mData.get(position).get("timer");
        
        lstTime = (ListenerTime) mData.get(position).get("listenerTime");
        lstNameInfo = (ListenerNameInfo) mData.get(position).get("listenerNameInfo");
        lstMore = (ListenerMore) mData.get(position).get("listenerMore");
        
        lstTime.setTag(holder.timer);
        lstNameInfo.setTag(holder.timer);
        lstMore.setTag(holder.timer);

        // 监听器
        holder.layoutTime.setOnClickListener(lstTime);
        holder.layoutNameAndInfo.setOnClickListener(lstNameInfo);
        holder.layoutMore.setOnClickListener(lstMore);
        //长按
        TimerLongClickListener LongListener = (TimerLongClickListener)mData.get(position).get("longListener");
        LongListener.setTag(holder.timer);
        holder.layoutNameAndInfo.setOnLongClickListener(LongListener);

        holder.time.setText(DateUtil.formatTwoNumber(holder.timer.getTimerDef().getStartHour()) + ":"
                + DateUtil.formatTwoNumber(holder.timer.getTimerDef().getStartMinute()));
        
        holder.name.setText(holder.timer.getName());
        
        String info;
        long now = System.currentTimeMillis();
        if(holder.timer.getNextTime() >= now)
        {
            info = DateUtil.MillisToYYYYMMDD(holder.timer.getNextTime()) + " "
                + DateUtil.MillisToHHMM(holder.timer.getNextTime());
                
            if(holder.timer.getTimerDef().isEnable() == 1)
            {
                holder.badge.setVisibility(View.VISIBLE);
                holder.badge.setText(DateUtil.intervalToString(holder.timer.getNextTime(), now));
            }
            else
            {
                holder.badge.setVisibility(View.GONE);
            }
        }
        else 
        {
            info = "已失效";
            holder.badge.setVisibility(View.GONE);
        }
        holder.info.setText(info);
        
        // 子布局
        setChildViewVisibility(holder, position);

        if (holder.visibility == View.GONE)
        {
            holder.more.setImageResource(R.drawable.arrow_down);
        } 
        else if (holder.visibility == View.VISIBLE)
        {
            holder.more.setImageResource(R.drawable.arrow_up);
        }

        if (holder.timer.getTimerDef().isEnable() == 0)
        {
            holder.time.setTextColor(Color.GRAY);
            holder.name.setTextColor(Color.GRAY);
            holder.info.setTextColor(Color.GRAY);

            holder.imageViewToggle.setImageResource(R.drawable.btn_toggle_off);
        } else
        {
            holder.time.setTextColor(Color.WHITE);
            holder.name.setTextColor(Color.WHITE);
            holder.info.setTextColor(Color.WHITE);

            holder.imageViewToggle.setImageResource(R.drawable.btn_toggle_on);
        }

        return convertView;
    }
    
    private void setChildViewVisibility(ViewHolder holder, final int position)
    {
        // 子布局
        holder.visibility = (Integer) mData.get(position).get("visibility");
        holder.parent = (ChildViewParent) mData.get(position).get("parent");
        holder.childView = (ChildView) mData.get(position).get("childView");
        if(holder.childView == null)
        {
            return;
        }
        
        holder.childView.setTag(holder.timer);

        if(holder.visibility != holder.layoutDown.getVisibility())
        {
            if(holder.visibility == View.VISIBLE)
            {
                if(!holder.mInit)
                {
                    // 子布局初始化
                    holder.childView.initLayout(holder.parent);
                    holder.childView.setParentScrollView((ScrollView)mData.get(position).get("scrollview"));
                    holder.layoutDown.addView(holder.childView.getLayoutView());
                    //必须要在 addView 之后才能 new ExpandAnimation
                    //每增加或者删除一个，就是调用new ViewHolder()
                    holder.expandAni = new ExpandAnimation(holder.layoutDown, ExpandAnimation.DEFAULT_DURATION);

                    holder.mInit = true;
                }
                else
                {
                    holder.childView.updateLayout();
                }
            }
            else
            {
                holder.childView.onClose();
            }
            
            holder.expandAni.reset();
            holder.expandAni.setVisibility(holder.visibility);
            holder.layoutDown.startAnimation(holder.expandAni);

        }
    }

}
